﻿using System;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class Itinerary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("SA2") < 0)
                {
                    Response.Redirect("Unauthorized.aspx");
                }

                long order_no;
                short pax_sn;
                short lang;
                try
                {

                    if (Request.QueryString["bk_id"] == null)
                    {
                        litTickets.Text = "Illegal page call!";
                        return;
                    }
                    order_no = Convert.ToInt64(Request.QueryString["bk_id"]);

                    if (Request.QueryString["pax_sn"] == null)
                    {
                        pax_sn = -1;
                    }
                    else
                    {
                        pax_sn = Convert.ToInt16(Request.QueryString["pax_sn"]);
                    }

                    if (Request.QueryString["lang"] == null)
                    {
                        lang = Convert.ToInt16(Session["LANG"].ToString());
                    }
                    else
                    {
                        lang = Convert.ToInt16(Request.QueryString["lang"]);
                    }

                }
                catch (Exception ex)
                {
                    litTickets.Text = ex.Message;
                    return;
                }
                Retreive(order_no, pax_sn, lang);

            }
        }

        protected void Retreive(long order_no, short pax_sn, short lang)
        {
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            if (pax_sn <= 0)
            {
                cmd.CommandText = "bll_bk_get_itinerary_info";
                para = cmd.Parameters.AddWithValue("@bk_id", order_no);
                para = cmd.Parameters.AddWithValue("@lang", lang);
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
            }
            else
            {
                cmd.CommandText = "bll_tkt_get_itinerary_info";
                para = cmd.Parameters.AddWithValue("@bk_id", order_no);
                para = cmd.Parameters.AddWithValue("@pax_sn", pax_sn);
                para = cmd.Parameters.AddWithValue("@lang", lang);
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@office_type", Session["OFFICE_TYPE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
            }
            DataSet dsRet = new DataSet();

            int last_pax_sn = -1, curr_pax_sn;//, last_trip_sn=-1;
            string curr_tkt_no = "", buf2;

            //Table tabReceipts;
            //TableRow tabRow;
            //TableCell tabCell;
            string sell_office = "", airline = "";
            string pay_currency = "", pay_time = "";
            StringBuilder buf = new StringBuilder();

            float total_fare = 0, fare_roe = 1;
            //float tkt_fare, taxes, fuel_charge, total_fare;

            try
            {
                TinyDAL.Exec(ref cmd, ref dsRet);
                if (dsRet.Tables.Count == 0)
                {
                    litTickets.Text = "Ticket not found, or invalid office!";
                    return;
                }
                foreach (DataRow row in dsRet.Tables[0].Rows)
                {
                    curr_pax_sn = Convert.ToInt32(row["pax_sn"]);
                    if (curr_pax_sn != last_pax_sn)
                    {
                        if (last_pax_sn != -1)
                        {
                            //Response.Write("<tr><td colspan='5' align='center'>" + bar_code128A(curr_tkt_no, 20, 1, 0) + "<br/>票号/Tkt No.: " + curr_tkt_no + "</td><td colspan='2'>总价/Total Fare: <br/>" + pay_currency + total_fare.ToString() + "</td><td colspan='3'>出票点/Agent:<br/>"/* + office_name + "(" */+ airline + "/" + sell_office + "</td><td colspan='3'>支付时间/Pay Time:<br/>" + pay_time + "</td></tr>");
                            buf.Append("<tr><td colspan='3' align='center'><img src='barcode.ashx?source=" + order_no + "&height=15&unit_width=1'><br/>订单号/Order No.: " + order_no + "</td><td colspan='3'>总价/Total Fare: <br/>" + pay_currency + total_fare.ToString() + "</td><td colspan='4'>出票点/Agent:<br/>"/* + office_name + "(" */+ airline + "/" + sell_office + "</td><td colspan='4'>支付时间/Pay Time:<br/>" + pay_time + "</td></tr>");
                            buf.Append("<tr><td colspan='14'>验真网站/Verify Site: http://www.NEO.com </td></tr>");
                            buf.Append("</table><br/>");
                            //查询EMD并输出......
                            buf.Append("<br/>" + get_emd_receipt(order_no, last_pax_sn, lang));

                            buf.Append("<div align='center'><img src='images/scissors.bmp'><!--hr style='border:1px dashed black; height:1px' width='50%'-->&nbsp;&nbsp;-------------------------------------------------------</div><br/>");
                            total_fare = 0;
                        }
                        buf.Append("<table style='border-collapse:collapse' border=1 borderColor='black'><tr><td colspan='14' align='center'><b><br/>行程提醒单/ITINERARY<br/>&nbsp;</b></td></tr><tr><td colspan='3' align='left'>姓名/Name:<br/>");
                        buf.Append(row["family_name"].ToString().Trim() + " " + row["name"].ToString().Trim() + "<br/>" + row["family_name_en"].ToString().Trim() + "/" + row["name_en"].ToString().Trim() + "</td>");
                        //Response.Write("<td colspan='2'>类型/Pax. Type:<br/>" + row["age_type"] + "</td><td colspan='7' align='center'>" + bar_code128A(row["id_no"].ToString(), 25, 1, 0) + "<br/>证件号/ID: " + row["id_no"].ToString().Trim() + "</td></tr>");
                        buf.Append("<td colspan='2'>年龄/Age:<br/>" + row["age_type"] + "</td><td colspan='2'>有效期至/Expiry:<br/>" + Convert.ToDateTime(row["expiry"]).ToShortDateString() + "</td><td colspan='7' align='center'><img src='barcode.ashx?source=" + row["id_no"].ToString() + "&height=20&unit_width=1'><br/>证件号/ID: " + row["id_no"].ToString().Trim() + "</td></tr>");
                        //Response.Write("<tr><td colspan='4'>支付时间/Pay Time:" + row["pay_time"].ToString() + "</td><td colspan='3'>支付币种/Currency:" + row["pay_currency"] + "</td><td colspan='3'>票价兑换率/Fare ROE:" + row["fare_roe"] + "</td><td colspan='3'>税兑换率/Tax ROE:" + row["tax_roe"] + "</td></tr>");
                        //Response.Write("<td colspan='2'>Order: " + Request.QueryString["bk_id"].ToString() + "</td></tr>");

                        //tabReceipts = new Table();
                        //tabRow = new TableRow();
                        //tabCell = new TableCell();
                        last_pax_sn = curr_pax_sn;
                        //last_trip_sn = -1;
                        buf.Append("<tr><td>S/N</td><td>航程<br/>From-To</td><td>航班<br/>Flight</td><td>日期<br/>Date</td><td>起飞时间<br/>Dep. At</td><td>到达时间<br/>Arr. At</td><td>舱号/子舱<br/>Cabin/Class</td><td>产品代码<br/>Fare Code</td><td>票价<br/>Price</td><td>税费<br/>Taxes&Surcharge</td><td>免费行李<br/>Bag. Allow</td><td>座位<br/>Seat</td><td>行李<br/>Baggage</td></tr>");//<tr><td>&nbsp;</td><td colspan='2'>Baggage Allow</td><td colspan='4'>Check-in No.</td></tr>");
                    }
                    //if(last_trip_sn==-1)
                    //    Response.Write("<tr><td>S/N</td><td>From-To</td><td>Flight</td><td>Cabin/Class</td><td>Dep. Time</td><td>Fare Code</td><td>Tkt. Price</td><td>Taxes</td><td>Fuel Charge</td></tr>");//<tr><td>&nbsp;</td><td colspan='2'>Baggage Allow</td><td colspan='4'>Check-in No.</td></tr>");

                    //tkt_fare = Convert.ToSingle(row["final_price"]);
                    //taxes = Convert.ToSingle(row["final_taxes"]);
                    //fuel_charge = Convert.ToSingle(row["final_fuel"]);
                    //total_fare = total_fare + tkt_fare + taxes + fuel_charge;
                    sell_office = row["pay_office"].ToString();
                    //office_name = row["name"].ToString().Trim();
                    //pay_time = row["pay_time"].ToString();
                    airline = row["pay_airline"].ToString();
                    buf2 = "000" + row["pax_sn"].ToString();
                    buf2 = buf2.Substring(buf2.Length - 3);
                    //order_no=Convert.ToInt64(row["bk_id"]);
                    curr_tkt_no = order_no.ToString() + buf2;
                    fare_roe = Convert.ToSingle(row["fare_roe"]);
                    //tax_roe = Convert.ToSingle(row["tax_roe"]);
                    pay_currency = row["pay_currency"].ToString();
                    pay_time = Convert.ToDateTime(row["pay_time"]).ToShortDateString();

                    buf.Append("<tr><td>" + row["trip_sn"].ToString() + "</td><td>" + row["ori_airport_name"].ToString().TrimEnd() + row["ori_terminal"] + " - " + row["des_airport_name"].ToString().TrimEnd() + row["des_terminal"] + "<br/>(" + row["ori_city"].ToString().TrimEnd() + "-" + row["des_city"].ToString().TrimEnd() + ")</td><td>" + row["airline_name"] + "<br/>" + row["airline"] + row["flt_no"] + "</td><td>" + Convert.ToDateTime(row["dep_time"]).ToShortDateString() + "</td>");
                    buf.Append("<td>" + Convert.ToDateTime(row["dep_time"]).ToShortTimeString() + "</td><td>" + Convert.ToDateTime(row["arr_time"]).ToShortTimeString() + "</td><td>" + row["cabin"] + "/" + row["class"] + "</td><td>" + row["fare_code"] + "</td><td>" + row["fare_currency"] + Convert.ToSingle(row["final_price"]).ToString() + "</td><td>" + row["fare_currency"] + Convert.ToSingle(row["final_surcharges"]).ToString() /*+ "</td><td>" + row["fare_currency"] + Convert.ToSingle(row["final_taxes"]).ToString()*/ + "</td>");
                    //total_fare = total_fare + (Convert.ToSingle(row["final_price"]) + Convert.ToSingle(row["final_fuel"]) /*+ Convert.ToSingle(row["other_fee"])*/) * Convert.ToSingle(row["fare_roe"]) + Convert.ToSingle(row["final_taxes"]) * Convert.ToSingle(row["tax_roe"]);
                    total_fare = total_fare + Convert.ToSingle(row["total_pay"]);
                    //Response.Write("<tr><td>&nbsp;</td><td>Baggage Allow:<br/>");
                    buf.Append("<td>" + row["final_free_bag"]);
                    //if (row["age_type"].ToString() == "A")
                    //    buf.Append(row["free_baggage"]);
                    //else if (row["age_type"].ToString() == "C")
                    //    buf.Append(row["chd_baggage"]);
                    //else if (row["age_type"].ToString() == "I")
                    //    buf.Append(row["inf_baggage"]);
                    buf.Append("</td><td>" + row["seat_row"].ToString() + row["seat_col"] + "</td><td>" + row["bag_count"].ToString() + "/" + row["bag_weight"].ToString());
                    //tkt_no = Request.QueryString["bk_id"].ToString() + "-" + row["pax_sn"].ToString().PadLeft(3, '0') + "-" + row["trip_sn"].ToString();
                    buf.Append("</td></tr>");//<td colspan='7' align='center'>" + bar_code128A(row["id_no"].ToString(), 25, 1, 0) + "<br/>Self-service Code: " + row["id_no"].ToString() + "</td></tr>");
                    buf.Append("<tr><td/><td colspan='13' align='left'>规则/Rules：<br/>" + row["fare_rule"].ToString().TrimEnd() + "</td></tr>");

                }
                //Response.Write("<tr><td colspan='5' align='center'>" + bar_code128A(curr_tkt_no, 20, 1, 0) + "<br/>票号/Tkt No.: " + curr_tkt_no + "</td><td colspan='2'>总价/Total Fare: <br/>" + pay_currency + total_fare.ToString() + "</td><td colspan='3'>出票点/Agent:<br/>"/* + office_name + "(" */+ airline + "/" + sell_office + "</td><td colspan='3'>支付时间/Pay Time:<br/>" + pay_time + "</td></tr>");
                buf.Append("<tr><td colspan='3' align='center'><img src='barcode.ashx?source=" + order_no + "&height=15&unit_width=1'><br/>订单号/Order No.: " + order_no + "</td><td colspan='3'>总价/Total Fare: <br/>" + pay_currency + total_fare.ToString() + "</td><td colspan='4'>出票点/Agent:<br/>"/* + office_name + "(" */+ airline + "/" + sell_office + "</td><td colspan='4'>支付时间/Pay Time:<br/>" + pay_time + "</td></tr>");
                buf.Append("<tr><td colspan='14'>验真网站/Verify Site: http://www.NEO.com </td></tr>");
                buf.Append("</table><br/>");
                //查询EMD并输出......
                buf.Append("<br/>" + get_emd_receipt(order_no, last_pax_sn, lang));

                buf.Append("&nbsp;<br/>");
                litTickets.Text = buf.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    litTickets.Text = ex.Message;
                else
                    litTickets.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected string get_emd_receipt(long order, int pax_sn, int lang)
        {
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            StringBuilder buf = new StringBuilder();
            DataTable dtRet = new DataTable();

            cmd.CommandText = "bll_tkt_get_emd_receipt";
            para = cmd.Parameters.AddWithValue("@bk_id", order);
            para = cmd.Parameters.AddWithValue("@pax_sn", pax_sn);
            para = cmd.Parameters.AddWithValue("@lang", lang);

            TinyDAL.Exec(ref cmd, ref dtRet);
            if (dtRet.Rows.Count == 0)
                return "";

            buf.Append("<table style='border-collapse:collapse' border='1' borderColor='black'><tr><td>延伸服务编号<br/>Service ID</td><td>服务名<br/>Service</td><td>数量<br/>Quantity</td><td>小计<br/>Subtotal</td><td>规则<br/>Rules</td></tr>");
            foreach (DataRow row in dtRet.Rows)
            {
                buf.Append("<tr><td>" + row["emd_id"].ToString() + "</td><td>" + row["emd_name"].ToString()
                + "</td><td>" + row["quantity"].ToString() + "</td><td align='right'>" + row["pay_currency"].ToString() + Convert.ToSingle(row["pay_total"]).ToString() + "</td><td align='left'>" + row["emd_note"].ToString().Trim() + "</td></tr>");
            }
            buf.Append("</table>");
            return buf.ToString();
        }
    }
}